import { _decorator, Component, Node, EventTouch, v2, Vec2, size, UITransform, EventMouse } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import GlobalData from '../../Init/Config/GlobalData';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { clog } from '../../Init/Tools/ColorLog';
import EventManager from '../../Init/Managers/EventManager';
const { ccclass, property } = _decorator;
const tmpP2_0 = v2();
const tmpP2_1 = v2();
const tmpP2_2 = v2();


@ccclass('LevelController')
export class LevelController extends BasicUI {
    /**触摸开关 */
    protected isEnableTouch: boolean = true;
    /**屏幕尺寸 */
    private _halfCvsSize = size();

    protected onLoad() {
        //系统触摸事件
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);

        this.initGesture();
        this.initMouse();
    }

    protected onEvents() {
        this.on(EventTypes.TouchEvents.SetTouchEnable, this.onSetTouchState, this);
    }

    public show(d?) {
        super.show(d);
        this.reset();
        const cvs = GlobalData.get(GlobalEnum.GlobalDataType.Canvas) as Node;
        this._halfCvsSize.set(cvs.getComponent(UITransform).contentSize);
        this._halfCvsSize.width *= 0.5;
        this._halfCvsSize.height *= 0.5;
    }

    public reset() {
        this.isEnableTouch = true;
    }

    //#region --- 触摸事件
    /**设置是否可触摸 */
    protected onSetTouchState(isEnable = false) {
        this.isEnableTouch = isEnable;
        console.log(isEnable ? '开启' : '关闭', '触摸');
    }

    private _tmpV1 = v2();
    private _tmpV2 = v2();
    protected onTouchStart(e: EventTouch) {
        if (!this.isEnableTouch) return;
        e.getUILocation(this._tmpV1); //初始touch位置        
        this._tmpV1.x -= this._halfCvsSize.width;
        this._tmpV1.y -= this._halfCvsSize.height;
        this.emit(EventTypes.TouchEvents.TouchStart, this._tmpV1);
        //
        this.emit(EventTypes.TouchEvents.TouchStartObj, e);
    }
    protected onTouchMove(e: EventTouch) {
        if (!this.isEnableTouch) return;
        e.getPreviousLocation(this._tmpV1)  //上一帧touch位置
        e.getUILocation(this._tmpV2);       //当前touch位置
        this._tmpV1.x -= this._halfCvsSize.width;
        this._tmpV1.y -= this._halfCvsSize.height;
        this._tmpV2.x -= this._halfCvsSize.width;
        this._tmpV2.y -= this._halfCvsSize.height;
        this.emit(EventTypes.TouchEvents.TouchMove, this._tmpV1, this._tmpV2);
        this.emit(EventTypes.TouchEvents.TouchMoveObj, e);
    }
    protected onTouchEnd(e: EventTouch) {
        if (!this.isEnableTouch) return;
        e.getStartLocation(this._tmpV1); //初始touch位置
        e.getUILocation(this._tmpV2);    //当前touch位置
        this._tmpV1.x -= this._halfCvsSize.width;
        this._tmpV1.y -= this._halfCvsSize.height;
        this._tmpV2.x -= this._halfCvsSize.width;
        this._tmpV2.y -= this._halfCvsSize.height;
        this.emit(EventTypes.TouchEvents.TouchEnd, this._tmpV1, this._tmpV2);
        this.emit(EventTypes.TouchEvents.TouchEndObj, e);
    }
    //#endregion

    //#region --- 手势识别
    @property({ tooltip: '双指捏合' })
    usePinch: boolean = false;
    //

    initGesture() {
        if (this.usePinch) {
            clog.tip('#开启双指捏合');
            this.node.on(Node.EventType.TOUCH_START, this.onPinchStart, this);
            this.node.on(Node.EventType.TOUCH_MOVE, this.onPinchMove, this);
            this.node.on(Node.EventType.TOUCH_END, this.onPinchEnd, this);
            this.node.on(Node.EventType.TOUCH_CANCEL, this.onPinchEnd, this);
        }

    }
    resetGesture() {

    }
    //#region --- 双指捏合
    _pinchScale: number = 0;
    _pinchTouchID = 0;
    _isPinchStart: boolean = false;
    _pinchCurLen: number = 0;
    _pinchStartLen: number = 0;
    _pinchCount = 0;

    onPinchStart(e: EventTouch) {
        const touchs = e.getAllTouches();
        this._pinchCount++;

        if (this._pinchCount > 2) return;

        if (this._pinchCount == 2 && touchs[0] && touchs[1]) {
            this._isPinchStart = true;
            this._pinchTouchID = e.getID();
            this._pinchScale = 1;
            // 计算长度
            touchs[0].getUILocation(tmpP2_0);
            touchs[1].getUILocation(tmpP2_1);

            this._pinchCurLen = tmpP2_1.subtract(tmpP2_0).length();
            this._pinchStartLen = this._pinchCurLen;

            // console.log('onPinchStart', this._pinchCurLen);
            EventManager.emit(EventTypes.TouchEvents.PinchGestureStart, this._pinchScale, e);
        }
    }
    // 
    onPinchMove(e: EventTouch) {
        if (!this._isPinchStart) return;

        const touchs = e.getAllTouches();

        if (this._pinchTouchID == e.getID() &&
            touchs[0] && touchs[1]) {
            // 计算长度
            touchs[0].getUILocation(tmpP2_0);
            touchs[1].getUILocation(tmpP2_1);

            const curLen = tmpP2_1.subtract(tmpP2_0).length();
            const subLen = curLen - this._pinchCurLen;
            this._pinchCurLen = curLen;
            this._pinchScale = this._pinchCurLen / this._pinchStartLen;

            // console.log('onPinchMove', subLen, this._pinchScale);
            EventManager.emit(EventTypes.TouchEvents.PinchGestureMove, subLen, e);
        }

    }

    onPinchEnd(e: EventTouch) {
        this._pinchCount--;

        if (this._pinchCount == 0) {
            // console.log('onPinchEnd', this._pinchScale);
            this._isPinchStart &&
                EventManager.emit(EventTypes.TouchEvents.PinchGestureEnd, this._pinchScale, e);
            this._isPinchStart = false;
        }
    }
    //#endregion

    //#endregion

    //#region --- Web端 鼠标事件
    initMouse() {

        // 监听鼠标滚轮
        this.node.on(Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }
    scrollYStep = 30;
    onMouseWheel(e: EventMouse) {
        const distY = e.getScrollY();
        const subLen = (distY > 0 ? 1 : -1) * this.scrollYStep;
        EventManager.emit(EventTypes.TouchEvents.PinchGestureMove, subLen, e);

    }
    //#endregion
}

